<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属ID" prop="kid">
        <el-input
          v-model="queryParams.kid"
          placeholder="请输入所属ID"
          clearable
          size="small"
          @valueup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格名" prop="attributeName">
        <el-input
          v-model="queryParams.attributeName"
          placeholder="请输入规格名"
          clearable
          size="small"
          @valueup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.key"
          placeholder="请选择规格"
          clearable
          size="small"
        >
          <el-option
            label="请选择字典生成"
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="display: block">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ds:value:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ds:value:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ds:value:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="valueList" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="所属id" align="center" prop="kid" sortable/>
      <el-table-column label="规格名称" align="center" prop="attributeValue" />
      <el-table-column label="启用/禁用" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#ff4949"
            inactive-color="#13ce66"
            :active-value="0"
            :inactive-value="1"
            @change="statusChange($event, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ds:value:edit']"
            >修改</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ds:value:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改规格信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属ID" prop="kid">
          <el-input v-model="form.kid" placeholder="请输入所属ID" />
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规格名" prop="attributeValue">
          <el-input v-model="form.attributeValue" placeholder="请输入规格名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listValue,
  getValue,
  delValue,
  addValue,
  updateValue,
  exportValue,
} from "@/api/ds/valueList";
import ImageUpload from "@/components/ImageUpload";

export default {
  name: "Value",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 规格信息表格数据
      valueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pid: null,
        attributeName: null,
        sort: null,
        image: null,
        isShow: null,
        addTime: null,
      },
      // 表单参数
      form: {
        id: null,
        pid: null,
        attributeName: null,
        params: {},
        status: null,
      },
      // 表单校验
      rules: {},
      statusOptions: [
        {
          // 规格下拉框
          value: "0",
          label: "禁用",
        },
        {
          value: "1",
          label: "启用",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询规格信息列表 */
    getList() {
      this.loading = true;
      listValue(this.queryParams).then((response) => {
        this.valueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pid: 12004,
        attributeName: null,
        params: {},
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加规格信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getValue(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改规格信息";
      });
    },
    /** 是否可登陆 */
    statusChange(e, row) {
      
      updateValue(row).then((response) => {
        this.msgSuccess("更新成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateValue(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.status = 1;
            addValue(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            this.form.status = null;
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除规格信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delValue(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有规格信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportValue(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },

    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      //rowIndex表示当前行号，从0开始，合计是第6行
      if (rowIndex !== 6) {
        //columnIndex表示当前列号，这里处理ID,姓名，数值2
        if (columnIndex === 0 || columnIndex === 1 || columnIndex === 3) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          //该形式为行合并
          return {
            rowspan: _row,
            colspan: _col,
          };
        }
      } else if (rowIndex == 6) {
        //处理合计，[1,2]表示合并1行2列，[0,0]表示改行不显示
        if (columnIndex === 0) {
          //定位到6行0列的ID，告诉该单元格合并1行2列
          return [1, 2];
        } else if (columnIndex === 1) {
          //定位到6行1列的姓名，告诉该单元格不显示
          return [0, 0];
        }
      }
    },
  },
};
</script>
